package com.smartalarmclock.Logic;

import java.util.Calendar;

import com.smartalarmclock.Model.ITimeFrame;
import com.smartalarmclock.Model.ITimeFrame.DayInWeek;


import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class ManageAlarmClocks {
	
	private static final int NUM_DAYS_IN_WEEK = 7;
	public static void AddClock(ITimeFrame i_TimeFrame, Context i_Context)
	{
		// Get schedule time
		DayInWeek day = i_TimeFrame.GetEndTime().Day;
		int hour = i_TimeFrame.GetEndTime().Time.getHours();
		int minutes = i_TimeFrame.GetEndTime().Time.getMinutes();
		
		// Set Calender instance for scheduled date
		Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(System.currentTimeMillis());
        int today = cal.get(Calendar.DAY_OF_WEEK);
        // Return correct days to add (considering the week day might have passed for the current week)
        
        int daysAddition = day.GetValue() >= today ? day.GetValue() - today : NUM_DAYS_IN_WEEK + day.GetValue() - today;
        if (daysAddition == 0 &&
        		((hour < cal.get(Calendar.HOUR_OF_DAY)) || (hour == cal.get(Calendar.HOUR_OF_DAY) && minutes <= cal.get(Calendar.MINUTE))))
        {
        	daysAddition = NUM_DAYS_IN_WEEK;
        }
        
        Log.v("Schedule Alarm", "Schedule request: hour: "+ hour + " minutes : " + minutes + "day: " +  day.GetValue());
        Log.v("Schedule Alarm", "Current time: hour: "+ cal.get(Calendar.HOUR_OF_DAY) + " minutes : " + cal.get(Calendar.MINUTE) + "day: " +  today);

        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH);
        int dayOfMonth = cal.get(Calendar.DAY_OF_MONTH); 
        cal.clear();
        cal.set(year, month, dayOfMonth, hour, minutes, 0);
        cal.add(Calendar.DATE, daysAddition);
        
        // Schedule
        AlarmManager alarmMgr = (AlarmManager)i_Context.getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(i_Context, AlarmClockBroadcastReceiver.class);
        // Send i_TimeFrame.GetId() in getBroadcast so I can use it to identify the alarm if I need it canceled. If it already exists, it will have no effect.
        PendingIntent pendingIntent = PendingIntent.getBroadcast(i_Context, i_TimeFrame.GetId(), intent, PendingIntent.FLAG_UPDATE_CURRENT);
        alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), AlarmManager.INTERVAL_DAY * NUM_DAYS_IN_WEEK, pendingIntent);
        
        Log.v("TimeSet", "Alarm set: " + cal.getTime().toString());
	}
	
	public static void RemoveClock(ITimeFrame i_TimeFrame, Context i_Context)
	{
		AlarmManager alarmMgr = (AlarmManager)i_Context.getSystemService(Context.ALARM_SERVICE);
		Intent intent = new Intent(i_Context, AlarmClockBroadcastReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(i_Context, i_TimeFrame.GetId(), intent, PendingIntent.FLAG_UPDATE_CURRENT);
		alarmMgr.cancel(pendingIntent);
	}
}
